<?php

namespace app\api\controller\haibao\jijin;

use app\admin\model\HaibaojijinLike;
use app\admin\model\HaibaojijinVideo;
use app\api\controller\haibao\HaibaoBase;
use app\api\controller\Utils;
use think\Request;
use app\common\model\Category;

/**
 * @author zero
 * @Time 2019/2/18
 *
 * Class Videos
 * @package app\api\controller\haibao\jijin
 *
 * 嗨爆集锦视频相关接口
 */
class Videos extends HaibaoBase
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->util = new Utils();
    }

    // 搜索建议词
    public function query_keyword()
    {
        $list = array('微信', '姑娘', '美味', '小姐姐', '有毒', '神走位', '是个人才', '历史', '卧槽', '可长点心吧', '哥们');
        $this->result('keyword', $list);
    }

    // 搜索视频
    public function query_video()
    {
        $keyword = $this->request->post('keyword');
        $page = $this->request->post('page') ?: 1;
        $list = HaibaojijinVideo::where('title', 'like', '%' . $keyword . '%')->limit(($page - 1) * 5, 5)->select();
        $this->result('query', $list);
    }

    // 获取分类
    public function get_category()
    {
        $category_list = [
            array(
                'id' => 28,
                'name' => '体育',
            ),
            array(
                'id' => 23,
                'name' => '搞笑',
            ),
            array(
                'id' => 22,
                'name' => '美女',
            ),
            array(
                'id' => 29,
                'name' => '生活',
            ),
            array(
                'id' => 31,
                'name' => '喜剧',
            ),
            array(
                'id' => 30,
                'name' => '科技',
            ),
        ];
        $this->result('category', $category_list);
    }

    // 获取推荐视频
    public function recommend_video()
    {
        $type = $this->request->post('type') ?: 'recommend';
        $category_id = $this->request->post('category_id') ?: null;

        if ($category_id) {
            $id_list = HaibaojijinVideo::where('category_id', $category_id)->where('duration', '<', '30')->column('id');
        } else {
            $id_list = HaibaojijinVideo::where('duration', '<', '30')->column('id');
        }

        $video = HaibaojijinVideo::get($id_list[array_rand($id_list)]);
        $video->height = '100vh';
        $video->width = '100%';
        $like = HaibaojijinLike::get(['user_id' => $this->user->id, 'like_id' => $video->id, 'delstatus' => 0]);
        $video->is_like = $like ? true : false;
        if ($video->protrait) {
            $video->objectFit = 'fill';
        } else {
            $video->objectFit = 'contain';
        }
        $this->result('recommend', $video);
    }

    // banner
    public function banner()
    {
        $list = array(
            array(
                'name' => '游趣娱乐',
                'appid' => 'wxa9002f27f3ed1519',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/haibao/banner/yqyl.jpg'
            ),
            array(
                'name' => '萌消团',
                'appid' => 'wxd26399f7e0a403d0',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/haibao/banner/mxt.jpg'
            ),
            array(
                'name' => '一起来合体',
                'appid' => 'wxd382be58d8c6d6c9',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/haibao/banner/heti.jpg'
            ),
            array(
                'name' => '一起飞飞飞',
                'appid' => 'wxe42ac5dd0f116b26',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/haibao/banner/yqfff.jpg'
            ),
            array(
                'name' => '切水果勇士',
                'appid' => 'wxaa2853914381ec98',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/haibao/banner/fruitwarrior.jpg'
            )
        );

        $this->result('banner', $list);
    }

    // 分类下的随机视频
    public function get_random_list_by_category()
    {
        $category_id = $this->request->post('category_id');

        $videos = HaibaojijinVideo::where('category_id', $category_id)->select();
        $random_keys = array_rand($videos, 8);
        $random_list = array();
        foreach ($random_keys as $key) {
            $videos[$key]->duration = $this->util->sec_to_time($videos[$key]->duration);
            array_push($random_list, $videos[$key]);
        }
        $this->result('random list', $random_list);
    }

    // 获取单个视频详细信息
    public function get_video_info()
    {
        $id = $this->request->post('id') ?: 1;
        $video = HaibaojijinVideo::get($id);
        $video->height = '100vh';
        $video->width = '100%';
        $like = HaibaojijinLike::get(['user_id' => $this->user->id, 'like_id' => $video->id, 'delstatus' => 0]);
        $video->is_like = $like ? true : false;
        if ($video->protrait) {
            $video->objectFit = 'fill';
        } else {
            $video->objectFit = 'contain';
        }
        $this->result('info', $video);
    }

    // 获取单个分类下的视频
    public function get_video_list_by_category()
    {
        $category_id = $this->request->post('category_id');
        $page = $this->request->post('page') ?: 1;

        $list = HaibaojijinVideo::where('category_id', $category_id)->limit(($page - 1) * 8, 8)->order('id', 'desc')->select();
        // 格式化数据
        $category = Category::get($category_id);
        foreach ($list as $item) {
            $item->category_id = $category;
            $item->duration = $this->util->sec_to_time($item->duration);
        }
        $this->result('list', $list);
    }

    // 获取随机视频列表
    public function get_random_list()
    {
        $list = $this->util->get_rand(5, 'haibao_video');
        $random_list = array();
        foreach ($list as $item) {
            $item['duration'] = $this->util->sec_to_time($item['duration']);
            $item['category_id'] = Category::get($item['category_id']);
            array_push($random_list, $item);
        }
        $this->result('random', array_values($random_list));
    }

}
